<!--  通知公告列表 -->
<template>
	<view class="main-contanier">
		<!-- 导航栏 -->
		<view class="nav-bar">
			<text class="back-icon" @click="goBack">←</text>
			<text class="header-title">通知公告</text>
		</view>
		<view class="main-box">
			<NoiceItem :list="listData" @clickItem="goDetail"></NoiceItem>
			<uni-load-more :status="loadingType" :icon-size="iconsize" :content-text="contentText"
				@clickLoadMore="clickLoadMore" />
		</view>
		<!-- 提示信息弹窗 -->
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	import {
		Platform,
		Storage_Key,
		ProjectStates,
		ProjectKeys
	} from '@/static/config.js';
	import request from '@/common/request.js';
	import NoiceItem from "@/pages/notice/components/notice-item/notice-item.vue"
	let that;
	export default {
		components: {
			NoiceItem
		},
		data() {
			return {
				// 数据列表
				listData: [],
				// 查询条件
				pageIndex: 1,
				pageSize: 10,
				doquerytime: Date.now(), // 每次执行初始查询时 将其置为当时时间，后续分页以该时间为基准进行查询。
				keyword: "",
				//加载更多
				refreshing: false,
				loadingType: 'more',
				iconsize: 16,
				contentText: {
					contentdown: '加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				wxUserInfo: [], //微信用户
				webUserInfo: [], //web平台用户
				// 提示信息弹窗
				msgType: 'success',
				messageText: '这是一条成功提示',
			}
		},
		beforeCreate() {
			that = this;
		},
		onLoad(options) {
			if (options && options.data) {}
			this.clear()
		},
		mounted() {
		},
		/**
		 *  点击导航栏 buttons 时触发
		 */
		onNavigationBarButtonTap() {
			// let title = this.dataInfo.pumpName;
			// this.toRunwork(this.dataInfo);
		},
		//加载更多
		onReachBottom() {
			this.pageIndex++;
			this.loadData();
		},
		onPullDownRefresh() { // 下拉刷新
			this.refreshing = true;
			this.pageIndex = 1;
			this.doquerytime = Date.now();
			this.loadingType = "more";
			this.listData = [];
			this.loadData().then(rs => {
				uni.stopPullDownRefresh();
				this.refreshing = false;
			});
		},
		computed: {},
		methods: {
			clear(e) {
				// this.wxUserInfo = this.$storage.get(Storage_Key.user_info, true);
				// this.webUserInfo = this.$storage.get(Storage_Key.web_user_info, true);
				this.pageIndex = 1;
				this.doquerytime = Date.now();
				this.loadingType = "more";
				this.listData = [];
				// this.keyword = "";
				// this.$nextTick(() => {
				// 	this.$refs.search.clear();
				// });
				//判断是否为游客，游客不能进行查询数据
				// if (!!this.wxUserInfo) {
				this.loadData();
				// }
			},
			loadData() {
				if (this.loadingType == "nomore") return;
				let _list = [];
				let totalcount = -1;
				let fromData = {
					pageIndex: this.pageIndex,
					pageSize: this.pageSize,
					// carCode: this.keyword, // carCode和contactphone，传单个只检索单个，多个或关系
					// contactphone: this.keyword,
				};
				// 权限分类（普通大众：只能查看自己的数据；平台管理员：查看所有数据）
				if (!this.webUserInfo) {
					fromData.openid = this.wxUserInfo.openId;
				}
				request({
					url: Platform.api_requestUrl + '/treatment/notice/list',
					data: fromData,
					method: 'GET',
				}).then(res => {
					if (res.code == 200 && !!res.rows) {
						_list = res.rows;
						this.listData = [...this.listData, ..._list];
						this.listData.forEach(element => {
							element.imageUrl = Platform.api_requestUrl + '/' + element.noticepath
						});
						totalcount = res.total;
					}
					if (this.listData.length >= totalcount)
						this.loadingType = "nomore"
				}).catch(error => {
					console.log("fail")
				});
			},
			// 关键字搜索
			search(e) {
				this.pageIndex = 1;
				this.doquerytime = Date.now();
				this.loadingType = "more";
				this.listData = [];
				this.keyword = e ? e.value : "";
				this.loadData();
			},
			input(e) {
				// 只处理清空
				if (!e.value) {
					this.search();
				}
			},
			goDetail(item) {
				let token = uni.getStorageSync('token');
				if(!token){
					this.messageToggle('error', '请登录后查看!')
					return;
				}
				
				uni.navigateTo({
					url: '/pages/notice/notice-detail?item=' + encodeURIComponent(JSON
						.stringify(item))
				})
			},
			// 点击加载更多
			clickLoadMore(e) {
				this.pageIndex++;
				this.loadData();
			},
			// 返回上一页
			goBack() {
				uni.navigateBack({
					delta: 1
				});
			},
			messageToggle(type, msg) {
				this.msgType = type;
				this.messageText = msg;
				this.$refs.message.open();
			},
		},
		filters: {

		}
	}
</script>
<style lang='scss'>
	.main-contanier {
		position: relative;
		background: #f5f5f5;
		min-height: 100vh;
	}

	.nav-bar {
		background: #f26522;
		height: 50px;
		display: flex;
		align-items: center;
		padding: 0 15px;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 999;
	}

	.back-icon {
		color: #fff;
		font-size: 24px;
		margin-right: 15px;
	}

	.header-title {
		color: #fff;
		font-size: 18px;
		font-weight: bold;
		flex: 1;
		text-align: center;
	}

	.main-box {
		padding-top: 50px;
		background: #FFFFFF;
		min-height: 100vh;
	}
</style>